﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Windows;
using Bookvar.ApplicationModel;
using Bookvar.ObjectModel;
using Bookvar.ServiceModel;

namespace BookvarWpf.Windows
{
    /// <summary>
    /// Interaction logic for ErrorWindow.xaml
    /// </summary>
    public partial class RecoveredMaps
    {
        private IDictionary<RecoveredMapAction, string> recoverActions;

        #region Properties

        [SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Used in bindings"
            )]
        public IDictionary<RecoveredMapAction, string> RecoverActions
        {
            get
            {
                if ( recoverActions == null )
                {
                    recoverActions = new Dictionary<RecoveredMapAction, string>
                                         {
                                             {
                                                 RecoveredMapAction.OpenRecoveredMap,
                                                 Bookvar.UI.Localization.Resources.RecoveredMapsOpenRecoveredMap
                                                 },
                                             {
                                                 RecoveredMapAction.DoNothing,
                                                 Bookvar.UI.Localization.Resources.RecoveredMapsDoNothing
                                                 }
                                         };
                }

                return recoverActions;
            }
        }

        private static IAutoSaveRecoveryService AutoSaveRecoveryService
        {
            get
            {
                return
                    BookvarApplication.Current.ServiceLayer.GetService<IAutoSaveRecoveryService>();
            }
        }

        public IEnumerable<RecoveredMapInfo> SelectedRecoveredMaps
        {
            get
            {
                return
                    from map in (RecoveredMap[]) lvRecoveredMaps.ItemsSource
                    where map.Action == RecoveredMapAction.OpenRecoveredMap
                    select map.RecoveredInfo;
            }
        }

        #endregion

        public RecoveredMaps()
        {
            InitializeComponent();

            BindRecoveredMaps();
        }

        private void BindRecoveredMaps()
        {
            var recoveredMaps = from mapInfo in AutoSaveRecoveryService.GetAutoSavedMaps()
                                select new RecoveredMap( mapInfo, RecoveredMapAction.OpenRecoveredMap );

            //call to array so we can later iterate over the same collection
            lvRecoveredMaps.ItemsSource = recoveredMaps.ToArray();
        }


        internal class RecoveredMap
        {
            private readonly RecoveredMapInfo info;
            private RecoveredMapAction action;

            [SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode",
                Justification = "Used in bindings" )]
            public RecoveredMapAction Action
            {
                get { return action; }
                set { action = value; }
            }

            [SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode",
                Justification = "Used in bindings" )]
            public string Name
            {
                get { return info.Name; }
            }

            internal RecoveredMapInfo RecoveredInfo
            {
                get { return info; }
            }

            public RecoveredMap( RecoveredMapInfo info, RecoveredMapAction action )
            {
                this.action = action;
                this.info = info;
            }
        }

        private void OnOkClick( object sender, RoutedEventArgs e )
        {
            DialogResult = SelectedRecoveredMaps.ToList().Count > 0;
            Close();
        }

        protected override void OnClosing( CancelEventArgs e )
        {
            base.OnClosing( e );

            //don't display recovered maps next time
            AutoSaveRecoveryService.ClearAutoSavedMaps();
        }
    }
}